package com.ruoyi.know.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.know.domain.CommunitySections;
import com.ruoyi.know.dto.TopPostDTO;
import com.ruoyi.know.mapper.CommunitySectionsMapper;
import com.ruoyi.know.mapper.UserFollowMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.know.mapper.CommunityPostsMapper;
import com.ruoyi.know.domain.CommunityPosts;
import com.ruoyi.know.service.ICommunityPostsService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 社区帖子Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-10
 */
@Service
public class CommunityPostsServiceImpl implements ICommunityPostsService
{
    @Autowired
    private CommunityPostsMapper communityPostsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private CommunitySectionsMapper communitySectionsMapper;
    @Autowired
    private UserFollowMapper userFollowMapper;

    /**
     * 查询社区帖子
     *
     * @param postId 社区帖子主键
     * @return 社区帖子
     */
    @Override
    public CommunityPosts selectCommunityPostsByPostId(Long postId)
    {
        CommunityPosts communityPosts = communityPostsMapper.selectCommunityPostsByPostId(postId);
        int visitQuantity = communityPosts.getVisitQuantity();
        communityPosts.setVisitQuantity(++visitQuantity);
        //增加浏览量
        communityPostsMapper.updateCommunityPosts(communityPosts);
        Long userId = communityPosts.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        communityPosts.setNickName(sysUser.getNickName());
        return communityPosts;
    }

    /**
     * 查询社区帖子列表
     *
     * @param communityPosts 社区帖子
     * @return 社区帖子
     */
    @Override
    public List<CommunityPosts> selectCommunityPostsList(CommunityPosts communityPosts)
    {
        List<CommunityPosts> posts = communityPostsMapper.selectCommunityPostsList(communityPosts);
        for (CommunityPosts communityPosts1 : posts){
            Long userId = communityPosts1.getUserId();
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            communityPosts1.setNickName(sysUser.getNickName());

            Long sectionId = communityPosts1.getSectionId();
            CommunitySections communitySections = communitySectionsMapper.selectCommunitySectionsBySectionId(sectionId);
            communityPosts1.setSectionName(communitySections.getSectionName());

            //  查询当前登录用户是否关注了该帖子
            LoginUser loginUser = (LoginUser) getLoginUser();
            boolean focused = userFollowMapper.getFocused(loginUser.getUserId(),communityPosts1.getUserId()) > 0;
            communityPosts1.setFocused(focused);
        }
        return posts;
    }

    /**
     * 新增社区帖子
     *
     * @param communityPosts 社区帖子
     * @return 结果
     */
    @Override
    public int insertCommunityPosts(CommunityPosts communityPosts)
    {
        return communityPostsMapper.insertCommunityPosts(communityPosts);
    }

    /**
     * 修改社区帖子
     *
     * @param communityPosts 社区帖子
     * @return 结果
     */
    @Override
    public int updateCommunityPosts(CommunityPosts communityPosts)
    {
        return communityPostsMapper.updateCommunityPosts(communityPosts);
    }

    /**
     * 批量删除社区帖子
     *
     * @param postIds 需要删除的社区帖子主键
     * @return 结果
     */
    @Override
    public int deleteCommunityPostsByPostIds(Long[] postIds)
    {
        return communityPostsMapper.deleteCommunityPostsByPostIds(postIds);
    }

    /**
     * 删除社区帖子信息
     *
     * @param postId 社区帖子主键
     * @return 结果
     */
    @Override
    public int deleteCommunityPostsByPostId(Long postId)
    {
        return communityPostsMapper.deleteCommunityPostsByPostId(postId);
    }

    @Override
    public int getCommunityPostsCount() {
        return communityPostsMapper.getCommunityPostsCount();
    }

    @Override
    public List<TopPostDTO> selectTop5PostsByVisit() {
        return communityPostsMapper.selectTop5PostsByVisit();
    }
}
